package com.lagou.homework;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.*;

import java.util.ArrayList;

public class KuduAPI {

    private static KuduClient client = new KuduClient.KuduClientBuilder("linux122").build();
    public static void main(String[] args) {
        createTable();
        delete();
        deleteTable();
        insert();
        select();
        update();
    }

    public  static void createTable() {
        String tableName = "student";
        //id int pkey    name String

        //指定每一列的信息
        ArrayList<ColumnSchema> columnSchemas = new ArrayList<ColumnSchema>();
        ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build();
        ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).build();
        columnSchemas.add(id);
        columnSchemas.add(name);
        Schema schema = new Schema(columnSchemas);

        CreateTableOptions options = new CreateTableOptions();
        //设置当前的副本数量为1
        options.setNumReplicas(1);
        ArrayList<String> colrule = new ArrayList<String>();
        colrule.add("id");
        options.addHashPartitions(colrule, 3);

        try {
            client.createTable(tableName, schema,options);
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
    public static void delete() {
        try {
            KuduTable stuTable = client.openTable("student");
            KuduSession kuduSession = client.newSession();
            kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
            Delete delete = stuTable.newDelete();
            PartialRow row = delete.getRow();
            row.addInt("id", 1);
            kuduSession.flush();
            kuduSession.apply(delete);
            kuduSession.close();
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
    public static void deleteTable() {
        try {
            client.deleteTable("student");
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
    public static void insert() {
        //2 、打开一张表
        try {
            KuduTable stuTable = client.openTable("student");
            //3 、创建会话
            KuduSession kuduSession = client.newSession();
            //4 、设置刷新模式
            kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
            //5 、获取插入实例
            Insert insert = stuTable.newInsert();
            //6 、声明带插入数据
            insert.getRow().addInt("id", 1);
            insert.getRow().addString("name", "lucas");
            //7 、刷入数据
            kuduSession.flush();
            //8 、应用插入实例
            kuduSession.apply(insert);
            //9 、关闭会话
            kuduSession.close();
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }
    public static void select() {
        try {
            KuduTable stuTable = client.openTable("student");
            //2 、获取Scanner
            KuduScanner scanner = client.newScannerBuilder(stuTable).build();
            //3 、从Scanner中循环遍历数据
            while (scanner.hasMoreRows()) {
                for (RowResult result : scanner.nextRows()) {
                    int id = result.getInt("id");
                    String name = result.getString("name");
                    System.out.println("id:" + id + "...name:" + name);
                }
            }
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

    public static void update() {
        try {
            KuduTable stuTable = client.openTable("student");
            KuduSession kuduSession = client.newSession();
            kuduSession.setFlushMode(SessionConfiguration.FlushMode.MANUAL_FLUSH);
            Update update = stuTable.newUpdate();
            PartialRow row = update.getRow();
            row.addInt("id", 1);
            row.addString("name", "xiaoming");
            kuduSession.apply(update);
            kuduSession.close();
        } catch (KuduException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (KuduException e) {
                e.printStackTrace();
            }
        }
    }

}
